<?php
namespace app\index\controller;

use think\Controller;

class Users extends Controller
{
	protected $usersModel;
	public function __construct(){
		$this->usersModel = new \app\common\model\Users();
	}

    public function index()
    {

    }
    //用户账号注册
    public function findByName()
    {
    	$user_name = input('username');
    	$bool = $this->usersModel->findByName($user_name);
    	echo json_encode($bool);

    }
    //用户账号注册
    public function register()
    {
    	
    	$bool = $this->usersModel->register();
    	echo json_encode($bool);

    }
    // 用户账号登陆
    public function login()
    {
    	

    	$bool = $this->usersModel->login();
    	 //dump($bool);die();
    	echo json_encode($bool);
    }
     public function restPassword()
    {
        

        $bool = $this->usersModel->restPassword();
         //dump($bool);die();
        echo json_encode($bool);
    }
    // 绑定设备
    public function bindDevice()
    {
    	$bool = Model('UsersDevice')->bindDevice();
    	 //dump($bool);die();
    	echo json_encode($bool);
    }
    // 获取用户绑定的设备列表的位置
    public function getDevice()
    {
    	$uid = input('uid');
    	if($uid){
    		$where = array('uid'=>$uid);
    		$content = Model('UsersDevice')
    					->alias('ud')
    					->field('ud.*,ds.longitude,ds.latitude,ds.type')
    					->join(config('prefix').'device_status ds',' ds.device_id=ud.did')
    					->where($where)
    					->select();
    		$count = Model('UsersDevice')
    					->alias('ud')
    					->field('ud.*,ds.longitude,ds.latitude,ds.type')
    					->join(config('prefix').'device_status ds',' ds.device_id=ud.did')
    					->where($where)
    					->count();
    		$out = array('status'=>'ok','msg'=>'成功','count'=>$count,'content'=>$content);
    	}else{   		
    		
    		$out = array('status'=>false,'msg'=>'缺少uid参数');
    	}
    	
    	echo json_encode($out);
    }

    public function getTelphone(){
        $from = input('from');
        $to = input('to');
        //$device = Model('device_status')->where(array('id'=>$from))->find();
        $user = Model('users')->where(array('id'=>$from))->find();
        // sendmsgToDevice($device['device_id'],$user['user_name']);
        sendmsgToDevice($to,$user['user_name']);
        $out = array('msg'=>'请稍等！','status'=>'ok');
        echo json_encode($out);exit();
    }

    public function getTrajectory(){
        $device_id = input('device_id');
        $date = input('date');
        if(!$device_id){
            $out = array('status'=>false,'msg'=>'缺少必须参数');
        }else{
            $where = array('device_id'=>$device_id,'date'=>$date,'type'=>1);
            $list = Model('device_message')
                    ->field('latitude,longitude,time,type')
                    ->where($where)
                    ->order('id asc')
                    ->select();
            $out = array('status'=>'ok','msg'=>'成功','content'=>$list);
        }
        echo json_encode($out);
    }

    
}
